//
//  LCHomeRetweetedView.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/6.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

class LCHomeRetweetedView: UIView {
    
    //记录约束
    private var currentConstanint:Constraint?
    
    //懒加载图片view
    var pictureView: LCHomePictureView = LCHomePictureView()
    
    
    //定义一个接口来接收传进来的model
    var retweetModel: LCHomeCellViewModel?{
        
        didSet{
            messageLabel.text = retweetModel?.contentMessage
            

            
            //先卸载已经存在约束
            currentConstanint?.uninstall()
            if let prictureList = retweetModel?.statusess?.retweeted_status?.pic_urls,prictureList.count>0{
                pictureView.isHidden = false
                
                self.snp_updateConstraints(closure: { (make) in
                    currentConstanint = make.bottom.equalTo(pictureView).offset(StatusItemMargin).constraint
                })
                //把数据传递给pictureview
                pictureView.prictureList = retweetModel?.statusess?.retweeted_status?.pic_urls
            }else{
                pictureView.isHidden = true
                
                self.snp_updateConstraints(closure: { (make) in
                    currentConstanint = make.bottom.equalTo(messageLabel).offset(StatusItemMargin).constraint
                })
            }

        }
    }
    
    //懒加载控件
    private var messageLabel:UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.text = "efefhofhuoehfiurhfriuhforhforhofrfrfrde"
        label.numberOfLines = 0
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    func setupUI(){
        addSubview(messageLabel)
        addSubview(pictureView)
        pictureView.backgroundColor = self.backgroundColor
        
        //设置约束
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(StatusItemMargin)
            make.leading.equalTo(self).offset(StatusItemMargin)
            make.width.equalTo(ScreenWidth - 2*StatusItemMargin)
        }
        //关键性约束
        self.snp_makeConstraints { (make) in
            currentConstanint = make.bottom.equalTo(pictureView).offset(StatusItemMargin).constraint
        }
        
        pictureView.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(StatusItemMargin)
            make.leading.equalTo(messageLabel)
//            make.size.equalTo(CGSize(width: 50, height: 50))
        })

    }

}
